package ro.dandanciu.webgallery.ui.users;

import org.apache.log4j.Logger;

import ro.dandanciu.webgallery.domainmodel.User;
import ro.dandanciu.webgallery.services.UserServices;
import ro.dandanciu.webgallery.ui.general.WebGalleryAction;
import ro.dandanciu.webgallery.util.Languages;

public class ProfileAction extends WebGalleryAction {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8932665107048980068L;

	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ProfileAction.class);
	
	private static final String VIEW = "view";

	private static final String VIEW_MODE = "VIEW";
	
	private static final String SAVE_MODE = "SAVE";
	
	private String mode = VIEW_MODE;
	
	private String userId = null;
	
	private String userName = null;
	
	private String email = null;
	
	private String password = null;
	
	private String passwordConfirmation = null;
	
	private boolean ignorePassword = true;
	
	private String language = Languages.ENGLISH.getCode();

	private UserServices userServices;
	
	
	public String execute() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start");
		}
		String forward = null;
	 	
		if(VIEW_MODE.equals(mode)) {
			forward = executeView();
	 	} else if(SAVE_MODE.equals(mode)) {
			forward = executeSave();
	 	} else {
	 		forward = setMessage("failure");
	 	}
	 

		if (logger.isDebugEnabled()) {
			logger.debug("forward: " + forward);
			logger.debug("execute() - end");
		}
		return forward;
	}

	private String executeSave() {
		//TODO check pass and email
		User user = getSessionUser();
		user.setEmail(email);
		user.setLanguage(language);
		user.setPassword(password);
		user.setUserName(userName);
		userServices.save(userId, user, ignorePassword, userId);
		return setMessage("profile.save.success");
	}

	private String executeView() {
		userId = getSessionUser().getUserId();
		User user = userServices.getUserByUserId(userId);
		userName = user.getUserName();
		email = user.getEmail();
		language = user.getLanguage();
		return VIEW;
	}
	
	public Languages[] getAvailableLanguages() {
		return Languages.values();
	}

	public String getMode() {
		return mode;
	}

	public void setMode(String mode) {
		this.mode = mode;
	}
	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void setUserServices(UserServices userServices) {
		this.userServices = userServices;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPasswordConfirmation() {
		return passwordConfirmation;
	}

	public void setPasswordConfirmation(String passwordConfirmation) {
		this.passwordConfirmation = passwordConfirmation;
	}

	public boolean isIgnorePassword() {
		return ignorePassword;
	}

	public void setIgnorePassword(boolean ignorePassword) {
		this.ignorePassword = ignorePassword;
	}

	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}
}
